package cn.easybuy.dao.impl;

import cn.easybuy.dao.ProductDao;
import cn.easybuy.entity.Product;
import cn.easybuy.utils.Utils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * 实现抽象方法
 */
public class ProductDaoImpl implements ProductDao {
    /**
     * 查询商所有商品信息
     * @return
     */
    @Override
    public List<Product> getAllProduct() {
        String sql = "SELECT * FROM easybuy_product";
        Connection connection = null;
        ResultSet rs = null;
        Product product =null;
        List<Product> list = new ArrayList<Product>();
        try{
            connection =  Utils.getConnection();
            rs =  Utils.select(sql,connection);
            while(rs.next()){
                product =new Product();
                product.setId(rs.getLong("id"));
                product.setName(rs.getString("name"));
                product.setDescription(rs.getString("description"));
                product.setPrice(rs.getFloat("price"));
                product.setStock(rs.getLong("stock"));
                product.setCategoryLevel1Id(rs.getLong("categoryLevel1Id"));
                product.setCategoryLevel2Id(rs.getLong("categoryLevel2Id"));
                product.setCategoryLevel3Id(rs.getLong("categoryLevel3Id"));
                product.setFileName(rs.getString("fileName"));
                product.setIsDelete(rs.getLong("isDelete"));
                list.add(product);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
           Utils.closeAll(connection,null,rs);
        }
        return list;
    }

    /**
     * 通过商品名字查询商品
     * @param name
     * @return
     */
    @Override
    public Product getProductByName(String name) {
        String sql = "SELECT * FROM easybuy_product WHERE name = ?";
        Connection connection = null;
        ResultSet rs = null;
        Product product = null;
        try{
            connection = Utils.getConnection();
            //System.out.println(name);
            rs = Utils.select(sql,connection,name);
            while(rs.next()){
                product =new Product();
                product.setId(rs.getLong("id"));
                product.setName(rs.getString("name"));
                product.setDescription(rs.getString("description"));
                product.setPrice(rs.getFloat("price"));
                product.setStock(rs.getLong("stock"));
                product.setCategoryLevel1Id(rs.getLong("categoryLevel1Id"));
                product.setCategoryLevel2Id(rs.getLong("categoryLevel2Id"));
                product.setCategoryLevel3Id(rs.getLong("categoryLevel3Id"));
                product.setFileName(rs.getString("fileName"));
                product.setIsDelete(rs.getLong("isDelete"));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            Utils.closeAll(connection,null,rs);
        }
        return product;

    }
    /**
     * 修改商品信息(只能通过id进行修改)
     * @param product
     * @return
     */
    @Override
    public int updateProduct(Product product) {
        String sql = "update easybuy_product set name=?,description=?,price=?,stock=?,categoryLevel1Id=?,categoryLevel2Id=?,categoryLevel3Id=?" +
                ",fileName=?,isDelete=? where id=? ";
        return Utils.execute(sql,product.getId());
    }

    /**
     * 添加商品
     * @param product
     * @return
     */
    @Override
    public int addProduct(Product product) {
        String sql = "INSERT INTO easybuy_product(name,description,price,stock,categoryLevel1Id,categoryLevel2Id,categoryLevel3Id,fileName,isDelete) values (?,?,?,?,?,?,?,?,?)";
        return Utils.execute(sql, product.getName(), product.getDescription(), product.getPrice(), product.getStock(), product.getCategoryLevel1Id(),
                product.getCategoryLevel2Id(), product.getCategoryLevel3Id(), product.getFileName(), product.getIsDelete());
    }
    /**
     * 通过商品id删除商品信息
     * @param id
     * @return
     */

    @Override
    public int deleteProduct(Long id) {
        String sql = "delete from easybuy_product where id=?";
        return Utils.execute(sql,id);
    }

    /**
     * 通过商品名称删除商品信息
     * @param name
     * @return
     */
    @Override
    public int deleteProduct(String name) {
        String sql = "delete from easybuy_product where name=?";
        return Utils.execute(sql,name);
    }

    @Override
    public List<Product> getProductBycategoryLevel1Id(Long categoryLevel1Id) {
        return null;
    }

    @Override
    public Product getProductBycategoryLevel2Id(Long categoryLevel2Id) {
        return null;
    }

    @Override
    public Product getProductBycategoryLevel3Id(Long categoryLevel3Id) {
        return null;
    }
}
